package com.wusongti.performance.script;

import static com.sun.btrace.BTraceUtils.*;

import com.sun.btrace.annotations.*;

/**
 * @ClassName: PrintAllLogScript
 * @author: wust
 * @date: 2022/04/28 17:03
 * @Description: 打印所有日志到脚本
 */
@BTrace
public class PrintAllLogScript {
    /**
     * @param null:
     * @Title: 监听com包下面的所有类和所有方法，方法返回时输出日志
     * @Description:
     * @author: wust
     * @return: null
     * @throws:
     */
    @OnMethod(clazz = "/com\\..*/", method = "/.*/",
        location = @Location(value = Kind.CALL, clazz = "/.*/", method = "/.*/", where = Where.AFTER))
    public static void com(@ProbeClassName String name, @TargetMethodOrField String method, @Duration long time) {

        String output = strcat(name, ".");
        output = strcat(output, method);
        output = strcat(output, "@@");
        output = strcat(output, str(probeLine()));
        output = strcat(output, "@@");
        output = strcat(output, str(time / 1000000));
        output = strcat(output, "@@");
        output = strcat(output, str(threadId(currentThread())));
        output = strcat(output, "@@");
        output = strcat(output, str(timeMillis()));
        println(output);
    }

    /**
     * @param null:
     * @Title: 监听org包下面的所有类和所有方法，方法返回时输出日志
     * @Description:
     * @author: wust
     * @return: null
     * @throws:
     */
    @OnMethod(clazz = "/org\\..*/", method = "/.*/",
        location = @Location(value = Kind.CALL, clazz = "/.*/", method = "/.*/", where = Where.AFTER))
    public static void org(@ProbeClassName String name, @TargetMethodOrField String method, @Duration long time) {

        String output = strcat(name, ".");
        output = strcat(output, method);
        output = strcat(output, "@@");
        output = strcat(output, str(probeLine()));
        output = strcat(output, "@@");
        output = strcat(output, str(time / 1000000));
        output = strcat(output, "@@");
        output = strcat(output, str(threadId(currentThread())));
        output = strcat(output, "@@");
        output = strcat(output, str(timeMillis()));
        println(output);
    }

}
